package com.rtx.api.utils;

import cn.hutool.core.util.StrUtil;

import java.io.IOException;

/**
 * 数据库类型验证-工具类
 *
 * @author Zy
 */
public class DatabaseClassUtils {

    public final static String DB_TYPE_ORACLE = "oracle";

    public final static String DB_TYPE_MYSQL = "mysql";

    /**
     * 获取数据库类型
     * 启动时 可能加载不到
     *
     * @return
     */
    public static String getDataBaseId() {

        return DB_TYPE_MYSQL;
        /*  框架默认使用mysql

        DataSource dataSource;
        WebApplicationContext appContext = ContextLoader.getCurrentWebApplicationContext();
        if (appContext == null) {

            // XXX spring boot 获取应用上下文
            ApplicationContext applicationContext = ApplicationContextRegister.getApplicationContext();
            dataSource = applicationContext.getBean(DataSource.class);
            VendorDatabaseIdProvider databaseIdProvider = (VendorDatabaseIdProvider) applicationContext.getBean("databaseIdProvider");
            return databaseIdProvider.getDatabaseId(dataSource);
        } else {

            dataSource = (DataSource) appContext.getBean("multipleDataSource");
            Object databaseIdProvider = appContext.getBean("databaseIdProvider");
            VendorDatabaseIdProvider vendorDatabaseIdProvider = (VendorDatabaseIdProvider) databaseIdProvider;
            return vendorDatabaseIdProvider.getDatabaseId(dataSource);
        }*/
    }

    /**
     * 判断是否是MySql
     *
     * @return
     * @throws IOException
     */
    public static boolean IsMySql() {
        return StrUtil.equalsIgnoreCase(DB_TYPE_MYSQL, DatabaseClassUtils.getDataBaseId());
    }

    /**
     * 判断是否是Oracle
     *
     * @return
     * @throws IOException
     */
    public static boolean isOracle() {
        return StrUtil.equalsIgnoreCase(DB_TYPE_ORACLE, DatabaseClassUtils.getDataBaseId());
    }
}
